var PR_HOME = {};

PR_HOME.requestType = {
    '1' : 'ยื่นประกวดราคา / เสนอราคา',
    '2' : 'วันที่เสนอราคา',
    '3' : 'เปลี่ยนแปลงผลิตภัณฑ์หลังยื่น Proposal',
    '4' : 'ขอเปิด PR',
    '5' : 'Presale',
    '6' : 'อื่นๆ'
};

PR_HOME.loadDataTable = function(isSearch) {
    
    var url = "../pr/searchForm";
    
    var columns =  [ {
	    "mData" : null,
	    "sClass" : "center"
	}, {
	    "mData" : null,
	    "sClass" : "center"
	}, {
	    "mData" : "requestNo",
	    "sClass" : "center"
	}, {
	    "mData" : "deptRequest",
	    "sClass" : "center"
	}, {
	    "mData" : "projectId",
	    "sClass" : "left"
	}, {
	    "mData" : "institution",
	    "sClass" : "left"
	}, {
	    "mData" : "projectNickname",
	    "sClass" : "left"
	}, {
	    "mData" : "requestType",
	    "sClass" : "left"
	}, {
	    "mData" : "jobRequireDateStr",
	    "sClass" : "center"
	}, {
	    "mData" : "deliveryDateStr",
	    "sClass" : "center"
	}, {
	    "mData" : "prNo",
	    "sClass" : "left"
	}, {
	    "mData" : "prDateStr",
	    "sClass" : "center"
	} ];

    $('#dataTable').dataTable(
		    {
			"bDestroy" : false,
			"bProcessing" : false,
			"bServerSide" : false,
			"bLengthChange" : false,
			"bFilter" : false,
			"bSort" : false,
			"bInfo" : false,
			"bAutoWidth" : false,
			"bJQueryUI" : false,
			"iDisplayLength" : 10,
			"iSortingCols" : 0,
			"sPaginationType" : "bootstrap",
//			"sAjaxSource" : url ,
			"sCharSet": "utf-8" ,
			"fnServerParams" : function(aoData) {
			    if(isSearch){
    			    	aoData.push({
    					"name" : "purchaseFormModel.requestNo",
    					"value" :  $('#requestNo').val()
    				    },{
    					"name" : "purchaseFormModel.projectId",
    					"value" :  $('#projectId').val()
    				    },{
    					"name" : "purchaseFormModel.institution",
    					"value" :  $('#institution').val()
    				    },{
    					"name" : "purchaseFormModel.projectNickname",
    					"value" :  $('#projectNickname').val()
    				    });
			    };
			},
			"fnRowCallback" : function(nRow, aData, iDisplayIndex) {
    			    $('td:eq(0)', nRow).html(
    				    '<input type="checkbox" name="keyDeletes" id="keyDeletes" value="'
				    + aData.requestNo + '" />');
			    $('td:eq(1)', nRow).html(iDisplayIndex+1);
			    $('td:eq(2)', nRow).html(
				    '<a href="javascript:PR_HOME.editByRequestNo(\''
					    + aData.requestNo + '\');">'
					    + aData.requestNo + '</a>');
    			    
    			    $('td:eq(7)', nRow).html(PR_HOME.requestType[aData.requestType]);
			},
			"aoColumns" : columns
		    });
};

PR_HOME.clearHeaderCriteria = function(){
    $('.required').unValidate();
    $('input#requestNo').clear();
    $('input#projectId').clear();
    $('input#institution').clear();
    $('input#projectNickname').clear();
};

//PR_HOME.getById = function(id) {
//    
//    $.ajax({
//	type : 'POST',
//	url : '../pr/edit',
//	dataType : 'json',
//	data : {
//	    requestNo : id
//	},
//	success : function(json) {
//	    console.log(json);
//	    // if (json.status == 'OK') {
//	    // $('#productCode').val(json.productTypeModel.productCode);
//	    // $('#productType').val(json.productTypeModel.productType);
//	    // $("#btnUpdate").show();
//	    // $("#btnSave").hide();
//	    // }
//	}
//    });
//};

PR_HOME.editByRequestNo = function(reqNo) {
//    console.log('edit?purchaseHeaderModel.requestPurchaseFormRequestNo='+reqNo);
//    PR_MAIN.purchaseHeaderModel['requestPurchaseFormRequestNo'] = reqNo;
    
    location.href = 'edit?requestNo='+reqNo;
};

PR_HOME.init = function() {
//	$("#btnSave").show();
//	$("#btnUpdate").hide();
};


$(document).ready(function() {
    
    
//    PR_HOME.loadDataTable();
    

    $("#btnSearch").click(function() {
    	$('.required').unValidate();
    	PR_HOME.loadDataTable(true);
    });
    
    $("#btnClear").click(function() {
	PR_HOME.clearHeaderCriteria();
    });
    
//    var reqNo = "<s:property value="param"/>";
//	console.log('reqNo : '+reqNo);
});
